package cn.wolfcode.web.controller;

import cn.wolfcode.domain.File;
import cn.wolfcode.domain.User;
import cn.wolfcode.service.IFileService;
import cn.wolfcode.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.util.List;

@RequestMapping("/chaxunServlet")
@Controller
public class chaxunController {
    @Autowired
    private IFileService fileService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ServletContext serverContext;
    @RequestMapping("/list")
    public ModelAndView list(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        Long uid = (Long)request.getSession().getAttribute("uid");
        List<File> list = fileService.selectuid(uid);
        User u = userService.selectid(uid);
        request.setAttribute("u",u);
        mv.addObject("list",list);
        mv.setViewName("forward:/WEB-INF/views/chaxun.jsp");
        return mv;
    }
    @RequestMapping("/delete")
    public ModelAndView delete(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        String strId = request.getParameter("id");
        fileService.delete(Long.parseLong(strId));
        mv.setViewName("forward:/chaxunServlet/list");
        return mv;
    }
    @RequestMapping("/zhuxiao")
    public ModelAndView zhuxiao(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        request.getSession().invalidate();
        mv.setViewName("forward:/static/index.jsp");
        return mv;
    }
    @RequestMapping("/input")
    public ModelAndView input(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        String id = request.getParameter("id");
        if(!id.equals(null)){
            long i = Long.parseLong(id);
            File f = fileService.selectid(i);
            request.setAttribute("f",f);
            mv.setViewName("forward:/WEB-INF/views/xiugai.jsp");

        }
        return mv;
    }
    @RequestMapping("/saveOrUpdate")
    public ModelAndView saveOrUpdate(MultipartFile pic,HttpServletRequest request) throws Exception {
        ModelAndView mv = new ModelAndView();
        String id = request.getParameter("id");
        long i = Long.parseLong(id);
        String filename=request.getParameter("filename");
        System.out.println(filename);
        serverContext.getRealPath("/upload");
        FileCopyUtils.copy(pic.getInputStream(),new FileOutputStream(serverContext.getRealPath("/upload")+"/"+pic.getOriginalFilename()));
        String filepath = "/upload/"+pic.getOriginalFilename();
        System.out.println(filepath);
        File file = new File();
        file.setFilename(filename);
        file.setFilepath(filepath);
        file.setId(i);
        fileService.update(file);
        mv.setViewName("forward:/chaxunServlet/list");
        return mv;
    }
    @RequestMapping("/input2")
    public ModelAndView input2(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/WEB-INF/views/tianjia.jsp");
        return mv;
    }
    @RequestMapping("/saveOrUpdate2")
    public ModelAndView saveOrUpdate2(MultipartFile pic,HttpServletRequest request) throws Exception {
        ModelAndView mv = new ModelAndView();
        Long uid =(Long)request.getSession().getAttribute("uid");
        String filename=pic.getOriginalFilename();
        serverContext.getRealPath("/upload");
        FileCopyUtils.copy(pic.getInputStream(),new FileOutputStream(serverContext.getRealPath("/upload")+"/"+pic.getOriginalFilename()));
        String filepath = "/upload/"+pic.getOriginalFilename();
        File file = new File();
        file.setFilename(filename);
        file.setFilepath(filepath);
        file.setUser_id(uid);
        fileService.insert(file);
        mv.setViewName("forward:/chaxunServlet/list");
        return mv;
    }
    @RequestMapping("/xiugaixinxi")
    public ModelAndView xiugaixinxi(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        Long uid = (Long)request.getSession().getAttribute("uid");
        User u = userService.selectid(uid);
        request.setAttribute("u",u);
        mv.setViewName("forward:/WEB-INF/views/xungaixinxi.jsp");
        return mv;
    }
    @RequestMapping("/xiugaixinxi1")
    public ModelAndView xiugaixinxi1(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        Long uid =(Long)request.getSession().getAttribute("uid");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        int age = Integer.parseInt(request.getParameter("age"));
        User u = new User();
        u.setPassword(password);
        u.setUsername(username);
        u.setAge(age);
        u.setId(uid);
        userService.update(u);
        mv.setViewName("forward:/chaxunServlet/list");
        return mv;
    }
}
